<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐患编码" prop="hdCode">
        <el-input
          v-model="queryParams.hdCode"
          placeholder="请输入隐患编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被处罚人id" prop="penalizedBy">
        <el-input
          v-model="queryParams.penalizedBy"
          placeholder="请输入被处罚人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被处罚人" prop="penalizedByName">
        <el-input
          v-model="queryParams.penalizedByName"
          placeholder="请输入被处罚人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入处罚金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚单位id" prop="punishDeptId">
        <el-input
          v-model="queryParams.punishDeptId"
          placeholder="请输入处罚单位id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚单位" prop="punishDept">
        <el-input
          v-model="queryParams.punishDept"
          placeholder="请输入处罚单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚人id" prop="punishBy">
        <el-input
          v-model="queryParams.punishBy"
          placeholder="请输入处罚人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚人" prop="punishByName">
        <el-input
          v-model="queryParams.punishByName"
          placeholder="请输入处罚人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚时间" prop="punishAt">
        <el-date-picker clearable
          v-model="queryParams.punishAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择处罚时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createByName">
        <el-input
          v-model="queryParams.createByName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createAt">
        <el-date-picker clearable
          v-model="queryParams.createAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updateByName">
        <el-input
          v-model="queryParams.updateByName"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateAt">
        <el-date-picker clearable
          v-model="queryParams.updateAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="排序" prop="sorting">
        <el-input
          v-model="queryParams.sorting"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dangerControl:punish:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dangerControl:punish:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dangerControl:punish:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dangerControl:punish:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="punishList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="隐患编码" align="center" prop="hdCode" />
      <el-table-column label="处罚内容" align="center" prop="content" />
      <el-table-column label="被处罚人id" align="center" prop="penalizedBy" />
      <el-table-column label="被处罚人" align="center" prop="penalizedByName" />
      <el-table-column label="处罚金额" align="center" prop="amount" />
      <el-table-column label="处罚单位id" align="center" prop="punishDeptId" />
      <el-table-column label="处罚单位" align="center" prop="punishDept" />
      <el-table-column label="处罚人id" align="center" prop="punishBy" />
      <el-table-column label="处罚人" align="center" prop="punishByName" />
      <el-table-column label="处罚时间" align="center" prop="punishAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.punishAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createByName" />
      <el-table-column label="创建时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateByName" />
      <el-table-column label="更新时间" align="center" prop="updateAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sorting" />
      <el-table-column label="备注说明" align="center" prop="remark" />
      <el-table-column label="状态, 默认1(1:正常；2:冻结；98:关闭；99:删除)；" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dangerControl:punish:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dangerControl:punish:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改隐患-处罚对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="隐患编码" prop="hdCode">
          <el-input v-model="form.hdCode" placeholder="请输入隐患编码" />
        </el-form-item>
        <el-form-item label="处罚内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="被处罚人id" prop="penalizedBy">
          <el-input v-model="form.penalizedBy" placeholder="请输入被处罚人id" />
        </el-form-item>
        <el-form-item label="被处罚人" prop="penalizedByName">
          <el-input v-model="form.penalizedByName" placeholder="请输入被处罚人" />
        </el-form-item>
        <el-form-item label="处罚金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入处罚金额" />
        </el-form-item>
        <el-form-item label="处罚单位id" prop="punishDeptId">
          <el-input v-model="form.punishDeptId" placeholder="请输入处罚单位id" />
        </el-form-item>
        <el-form-item label="处罚单位" prop="punishDept">
          <el-input v-model="form.punishDept" placeholder="请输入处罚单位" />
        </el-form-item>
        <el-form-item label="处罚人id" prop="punishBy">
          <el-input v-model="form.punishBy" placeholder="请输入处罚人id" />
        </el-form-item>
        <el-form-item label="处罚人" prop="punishByName">
          <el-input v-model="form.punishByName" placeholder="请输入处罚人" />
        </el-form-item>
        <el-form-item label="处罚时间" prop="punishAt">
          <el-date-picker clearable
            v-model="form.punishAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择处罚时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="createByName">
          <el-input v-model="form.createByName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createAt">
          <el-date-picker clearable
            v-model="form.createAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updateByName">
          <el-input v-model="form.updateByName" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateAt">
          <el-date-picker clearable
            v-model="form.updateAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排序" prop="sorting">
          <el-input v-model="form.sorting" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPunish, getPunish, delPunish, addPunish, updatePunish } from "@/api/dangerControl/punish";

export default {
  name: "Punish",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患-处罚表格数据
      punishList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        hdCode: null,
        content: null,
        penalizedBy: null,
        penalizedByName: null,
        amount: null,
        punishDeptId: null,
        punishDept: null,
        punishBy: null,
        punishByName: null,
        punishAt: null,
        createByName: null,
        createAt: null,
        updateByName: null,
        updateAt: null,
        sorting: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询隐患-处罚列表 */
    getList() {
      this.loading = true;
      listPunish(this.queryParams).then(response => {
        this.punishList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        hdCode: null,
        content: null,
        penalizedBy: null,
        penalizedByName: null,
        amount: null,
        punishDeptId: null,
        punishDept: null,
        punishBy: null,
        punishByName: null,
        punishAt: null,
        createBy: null,
        createByName: null,
        createAt: null,
        updateBy: null,
        updateByName: null,
        updateAt: null,
        sorting: null,
        remark: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加隐患-处罚";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPunish(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改隐患-处罚";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePunish(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPunish(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除隐患-处罚编号为"' + ids + '"的数据项？').then(function() {
        return delPunish(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dangerControl/punish/export', {
        ...this.queryParams
      }, `punish_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
